//package com.spider.im.chat.dao;
//
//import com.spider.im.chat.model.ChatGroupUser;
//import com.spider.im.chat.utils.DatabaseManager;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.sql.*;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.List;
//
//public class ChatGroupUserDAO {
//    private static final Logger logger = LoggerFactory.getLogger(ChatGroupUserDAO.class);
//
//    // 插入一个新的ChatGroupUser
//    public boolean insertChatGroupUser(ChatGroupUser chatGroupUser) {
//        String sql = "INSERT INTO chat_group_user (chat_group_id, user_id, user_status, user_role, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)";
//        try (Connection conn = DatabaseManager.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(sql)) {
//            stmt.setLong(1, chatGroupUser.getChatGroupId());
//            stmt.setLong(2, chatGroupUser.getUserId());
//            stmt.setString(3, chatGroupUser.getUserStatus());
//            stmt.setString(4, chatGroupUser.getUserRole().name());
//            stmt.setTimestamp(5, Timestamp.valueOf(chatGroupUser.getCreatedAt()));
//            stmt.setTimestamp(6, Timestamp.valueOf(chatGroupUser.getUpdatedAt()));
//            int rowsAffected = stmt.executeUpdate();
//            return rowsAffected > 0;
//        } catch (SQLException e) {
//            logger.error("Error inserting chat group user: {}", e.getMessage());
//            return false;
//        }
//    }
//
//    // 根据ID查询ChatGroupUser
//    public ChatGroupUser getChatGroupUserById(Long id) {
//        String sql = "SELECT * FROM chat_group_user WHERE id = ?";
//        try (Connection conn = DatabaseManager.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(sql)) {
//            stmt.setLong(1, id);
//            try (ResultSet rs = stmt.executeQuery()) {
//                if (rs.next()) {
//                    return mapResultSetToChatGroupUser(rs);
//                }
//            }
//        } catch (SQLException e) {
//            logger.error("Error getting chat group user by id: {}", e.getMessage());
//        }
//        return null;
//    }
//
//    // 查询某个聊天组的所有用户
//    public List<ChatGroupUser> getUsersByChatGroupId(Long chatGroupId) {
//        List<ChatGroupUser> users = new ArrayList<>();
//        String sql = "SELECT * FROM chat_group_user WHERE chat_group_id = ?";
//        try (Connection conn = DatabaseManager.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(sql)) {
//            stmt.setLong(1, chatGroupId);
//            try (ResultSet rs = stmt.executeQuery()) {
//                while (rs.next()) {
//                    users.add(mapResultSetToChatGroupUser(rs));
//                }
//            }
//        } catch (SQLException e) {
//            logger.error("Error getting users by chat group id: {}", e.getMessage());
//        }
//        return users;
//    }
//
//    // 更新ChatGroupUser
//    public boolean updateChatGroupUser(ChatGroupUser chatGroupUser) {
//        String sql = "UPDATE chat_group_user SET user_status = ?, user_role = ?, updated_at = ? WHERE id = ?";
//        try (Connection conn = DatabaseManager.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(sql)) {
//            stmt.setString(1, chatGroupUser.getUserStatus());
//            stmt.setString(2, chatGroupUser.getUserRole().name());
//            stmt.setTimestamp(3, Timestamp.valueOf(chatGroupUser.getUpdatedAt()));
//            stmt.setLong(4, chatGroupUser.getId());
//            int rowsAffected = stmt.executeUpdate();
//            return rowsAffected > 0;
//        } catch (SQLException e) {
//            logger.error("Error updating chat group user: {}", e.getMessage());
//            return false;
//        }
//    }
//
//    // 删除ChatGroupUser
//    public boolean deleteChatGroupUser(Long id) {
//        String sql = "DELETE FROM chat_group_user WHERE id = ?";
//        try (Connection conn = DatabaseManager.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(sql)) {
//            stmt.setLong(1, id);
//            int rowsAffected = stmt.executeUpdate();
//            return rowsAffected > 0;
//        } catch (SQLException e) {
//            logger.error("Error deleting chat group user: {}", e.getMessage());
//            return false;
//        }
//    }
//
//    // 将ResultSet映射为ChatGroupUser对象
//    private ChatGroupUser mapResultSetToChatGroupUser(ResultSet rs) throws SQLException {
//        ChatGroupUser chatGroupUser = new ChatGroupUser();
//        chatGroupUser.setId(rs.getLong("id"));
//        chatGroupUser.setChatGroupId(rs.getLong("chat_group_id"));
//        chatGroupUser.setUserId(rs.getLong("user_id"));
//        chatGroupUser.setUserStatus(rs.getString("user_status"));
//        chatGroupUser.setUserRole(ChatGroupUser.UserRole.valueOf(rs.getString("user_role")));
//        chatGroupUser.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
//        chatGroupUser.setUpdatedAt(rs.getTimestamp("updated_at").toLocalDateTime());
//        return chatGroupUser;
//    }
//
//    public static void main(String[] args) {
//        ChatGroupUserDAO chatGroupUserDAO = new ChatGroupUserDAO();
//        ChatGroupUser chatGroupUser = new ChatGroupUser(2L, 3L, "active", ChatGroupUser.UserRole.OWNER, LocalDateTime.now(), LocalDateTime.now());
//        try {
//            if (chatGroupUserDAO.insertChatGroupUser(chatGroupUser)) {
//                System.out.println("ChatGroupUser inserted successfully.");
//            } else {
//                System.out.println("Failed to insert ChatGroupUser.");
//            }
//        }
//        catch (Exception e) {
//            System.out.println("Error: " + e.getMessage());
//        }
//    }
//}
